#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import division, unicode_literals

from django.shortcuts import render
from django.contrib.auth import authenticate
from django.contrib import auth, messages
from django.shortcuts import redirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from datetime import datetime
from manager import forms


def login(request):
    if request.method == 'POST':
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            login_name = request.POST['username'].strip()
            login_password = request.POST['password']
            user = authenticate(username=login_name, password=login_password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    messages.add_message(request, messages.SUCCESS, '成功登录')
                    return redirect('/')
                else:
                    messages.add_message(request, messages.WARNING,
                                         '帐号尚未启用')
            else:
                messages.add_message(request, messages.WARNING,
                                     '登录失败，用户名或者密码错误')
        else:
            messages.add_message(request, messages.INFO, '请检查输入的字段内容')
    else:
        login_form = forms.LoginForm()

    request_context = RequestContext(request)
    request_context.push(locals())
    return render(request, 'manager/login.html', request_context)


def logout(request):
    auth.logout(request)
    messages.add_message(request, messages.INFO, "注销成功")
    return redirect('/login')


@login_required(login_url='/login/')
def changepassword(request):
    username = request.user.username

    if request.method == "POST":
        change_form = forms.ChangePasswdForm(request.POST)
        if change_form.is_valid():
            old_password = request.POST['old_password']
            new1_password = request.POST['new1_password']
            new2_password = request.POST['new2_password']

            if new1_password == new2_password:
                user = authenticate(username=username, password=old_password)
                if user is not None:
                    try:
                        new_password = make_password(new1_password)
                        user = User.objects.filter(username=username)\
                            .update(password=new_password)
                    except Exception:
                        messages.add_message(request, messages.WARNING,
                                             '操作失败')
                    else:
                        messages.add_message(request, messages.SUCCESS,
                                             '修改密码成功')
                else:
                    messages.add_message(request, messages.WARNING,
                                         '验证旧密码失败')
            else:
                messages.add_message(request, messages.WARNING,
                                     '两次输入的新密码不一致')
        else:
            messages.add_message(request, messages.INFO,
                                 '请检查输入的字段内容')

    else:
        change_form = forms.ChangePasswdForm()

    request_context = RequestContext(request)
    request_context.push(locals())
    return render(request, 'manager/changepassword.html', request_context)


@login_required(login_url='/login/')
def index(request):
    now = datetime.now()
    username = request.user.username
    return render(request, 'manager/index.html', locals())
